package org.mili.ticketeer.ui.ticket;

import org.mili.ticketeer.dto.Action;
import org.mili.ticketeer.dto.Ticket;
import org.mili.ticketeer.service.ActionService;
import org.mili.application.service.ServiceException;
import org.mili.application.service.ServiceFactory;
import org.mili.ticketeer.service.TicketService;
import org.mili.application.ui.OneToManyTableModel;

import java.util.List;

/**
 */
public class TicketActionTableModel extends OneToManyTableModel<Ticket, Action> {
    private TicketService ticketService = ServiceFactory.getService(TicketService.class);
    private ActionService actionService = ServiceFactory.getService(ActionService.class);
    private Ticket ticket;

    public TicketActionTableModel() {
        super(new String[]{"No.", "Title", "Date"});
    }

    @Override
    public List<Action> load(Ticket ticket) {
        try {
            return actionService.getActionsFor(ticket);
        } catch (ServiceException e) {
            throw new IllegalStateException(e);
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Action action = get(rowIndex);
        switch(columnIndex) {
            case 0:
                return rowIndex + 1;
            case 1:
                return action.getTitle();
            case 2:
                return action.getDate();
            default: throw new IllegalStateException("no column index for: " + columnIndex);
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return Integer.class;
        }
        return super.getColumnClass(columnIndex);
    }

}
